package com.huobao.yunying.adapters;

import java.util.List;

import com.huobao.yunying.entity.GouwucgeGoods;
import com.huobao.yunying.entity.Gouwuche_spec;
import com.huobao.yunying.entity.Gouwuche_specification;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.zhangying.R;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class Adapter_jiesuan_item extends QuickAdapter<GouwucgeGoods> {

	public Adapter_jiesuan_item(Context context, List<GouwucgeGoods> data) {
		super(context, R.layout.item_gouwuche_child_view, data);
	}

	@Override
	protected void convert(BaseAdapterHelper arg0, GouwucgeGoods goods) {
		CheckBox checkbox = arg0.getView(R.id.checkbox);
		// 上架logo
		ImageView imageview = arg0.getView(R.id.imageview);
		// 商品名字
		TextView tv_goods_name = arg0.getView(R.id.tv_goods_name);
		// 单价
		TextView tv_price = arg0.getView(R.id.tv_price);
		// 选择多件的时候显示得View
		View llyt_allcount = arg0.getView(R.id.llyt_allcount);
		// 一共多少件
		TextView tv_count = arg0.getView(R.id.tv_count);
		// 规格
		LinearLayout llyt_guige = arg0.getView(R.id.llyt_guige);

		// 选择单件的显示得View
		View view_dan = arg0.getView(R.id.llyt);

		// 总价
		TextView tv_allprice = arg0.getView(R.id.tv_allprice);
		// 删除
		TextView tv_del = arg0.getView(R.id.tv_del);
		// 编辑
		TextView tv_edit = arg0.getView(R.id.tv_edit);
		tv_del.setVisibility(View.GONE);
		tv_edit.setVisibility(View.GONE);
		view_dan.setVisibility(View.GONE);
		tv_count.setVisibility(View.GONE);
		llyt_allcount.setVisibility(View.GONE);
		checkbox.setVisibility(View.GONE);

		ImageUtils.loadimg(imageview, goods.goods_image);
		tv_goods_name.setText(goods.goods_name);
		tv_allprice.setText("￥" + goods.subtotal);
		tv_price.setText("共" + goods.quantity
				+ (goods.unit == null || goods.unit.equals("") || goods.unit.equals("null") ? "" : goods.unit));

		if (goods.specification != null && goods.specification.size() != 0) {
			showGuige(llyt_guige, goods.specification);
		} else {
			llyt_guige.removeAllViews();
		}

	}

	private void showGuige(LinearLayout llyt_guige, List<Gouwuche_specification> specification) {
		llyt_guige.removeAllViews();
		for (int i = 0; i < specification.size(); i++) {
			Gouwuche_specification gouwuche_specification = specification.get(i);
			LinearLayout layout = new LinearLayout(context);
			layout.setOrientation(LinearLayout.HORIZONTAL);
			layout.setGravity(Gravity.CENTER_VERTICAL);
			LayoutParams layoutParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
			layoutParams.weight = 1;
			layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
			layout.addView(createNameTextView(gouwuche_specification.spec_1), layoutParams);

			layout.addView(createHLine());
			// 生成右边的规格
			LinearLayout linearLayout = new LinearLayout(context);
			linearLayout.setOrientation(LinearLayout.VERTICAL);
			List<Gouwuche_spec> spec = gouwuche_specification.spec;
			for (Gouwuche_spec gouwuche_spec : spec) {
				linearLayout.addView(createValueTextView(
						gouwuche_spec.spec_2 + "		" + gouwuche_spec.num + "*" + gouwuche_spec.price));
				linearLayout.addView(createLine());
			}
			layoutParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
			layoutParams.weight = 1;
			layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
			layout.addView(linearLayout, layoutParams);

			llyt_guige.addView(createLine());
			llyt_guige.addView(layout);
		}
	}

	private View createHLine() {
		View view = new View(context);
		view.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
		view.setBackgroundColor(Color.parseColor("#dddddd"));
		return view;
	}

	private View createLine() {
		View view = new View(context);
		view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
		view.setBackgroundColor(Color.parseColor("#dddddd"));
		return view;
	}

	/**
	 * 生成标题文字
	 * 
	 * @param text
	 * @return
	 */
	private TextView createNameTextView(String text) {
		TextView textView = new TextView(context);
		textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		textView.setText(text);
		textView.setTextSize(16);
		// textView.setPadding(0, 8, 0, 8);
		return textView;
	}

	private TextView createValueTextView(String text) {
		TextView textView = new TextView(context);
		textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		textView.setText(text);
		textView.setTextSize(14);
		return textView;
	}

}
